package main

import (
	pb "fgo/advance/grpc/grpc-gateway/proto/hello" // 引入proto包
	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
	"log"
	"time"

	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/grpclog"
)

const (
	// Address gRPC服务地址
	Address = "127.0.0.1:50052"
)

func main() {
	// 连接
	conn, err := grpc.Dial(Address,
		grpc.WithInsecure(),
		grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
		grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
	) // grpc.WithBalancerName("abc")

	if err != nil {
		grpclog.Fatalln(err)
	}

	defer conn.Close()

	// 初始化客户端
	c := pb.NewHelloClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	// 调用方法
	r, err := c.SayHello(ctx, &pb.HelloRequest{
		Name: "gRPC",
	})
	if err != nil {
		grpclog.Fatalln(err)
	}
	log.Println(r.Message)

}
